﻿--赛马活动领奖人（赤焰峡谷）

--#include "RankPointer.lua" once	--排行榜指针
--#include "data\functions\Activity\Common\Consumes.lua" once --活动通用 消耗
--#include "data\functions\Activity\Common\Awards.lua" once --活动通用 奖励
--#include "data\functions\Activity\Common\Scene.lua" once --活动通用 场景

--[[
    函数名称：传送
    函数描述：NPC交互入口
    参数 sysarg 的描述：
]]
function EndHorseRace_Main(sysarg)	
	local config = HorseRaceConfig
	if (not config) then return "" end
	if (not HorseRaceRankPt) then return "" end
	local str = Lang.Activity.t00149
	str = str..Lang.Activity.t00150
	local pRank = Ranking.getRanking(HorseRaceRankPt.RankName)
	if pRank then
		local rankList = LuaHelp.getRankingItemList(pRank, 3)
		if not rankList then
			str = str..Lang.Activity.t00151
		else
			for k, v in pairs(rankList) do
				local name = Ranking.getSub(v, 0)
				str = str..string.format(Lang.Activity.t00152, k, name)
			end
		end
		local userId = Actor.getIntProperty(sysarg, PROP_ENTITY_ID)
		local rank = Ranking.getItemIndexFromId(pRank, userId)+1
		if rank > 0 then
			str = str..string.format(Lang.Activity.t00153, rank)
		else
			str = str..Lang.Activity.t00154
		end
	end
	
	str = str.."\n<#BL"..Lang.Activity.t00155.."/@GetHorseRaceAward>"  --<领取活动奖励>
	
	return str
end


--领取本层奖励
function GetHorseRaceAward(sysarg)
	local config = HorseRaceConfig
	
	--[[
	local dval = System.getDyanmicVar()
	
	if dval.HorseRaceStart ~= 1 then
		Actor.sendTipmsg(sysarg, "活动还没有开启", ttFlyTip)
		return 
	end
	--]]
	
	local pRank = Ranking.getRanking(HorseRaceRankPt.RankName)
	if not pRank then
		return 
	end
	local index = 1
	local userId = Actor.getIntProperty(sysarg, PROP_ENTITY_ID)
	local pItem = Ranking.getItemPtrFromId(pRank, userId)
	
	local nRank = Ranking.getItemIndexFromId(pRank, userId)+1
	if nRank <= 0 then
		Actor.sendTipmsg(sysarg, Lang.Activity.t00156, ttFlyTip)
		return 
	end
	
	if Ranking.getSub(pItem, index) ~= "-" then
		Actor.sendTipmsg(sysarg, Lang.Activity.t00157, ttFlyTip)
		return 
	end

	local nRate = GetCombineActivityRate()

	--排行榜奖励
	if config.rankAward and table.getn(config.rankAward) > 0 then
		for k, v in pairs(config.rankAward) do
			if nRank >= v.rankInterval[1] and nRank <= v.rankInterval[2] then
				if Awards.CheckBagGridCount(sysarg, v.awards) ~= true then return end
				--设置领取标志
				Ranking.setSub(pItem, index, 1)
				
				for i=1,table.getn(v.awards) do
					local awardTab = v.awards[i]
					Actor.giveAward(sysarg, awardTab.type,awardTab.id,awardTab.count * nRate,awardTab.quality,awardTab.strong,awardTab.bind,0,100,Lang.Activity.l00085)
				end

				--Awards.Give(sysarg, v.awards, 100, Lang.Activity.l00085)
				Actor.sendTipmsg(sysarg, Lang.Activity.t00158, ttFlyTip)
				
				--刷个BOSS
				if nRank == 1 and config.winBoss then
					local boss = config.winBoss
					local hScene = Actor.getSceneHandle(sysarg)
					Fuben.createMonstersInRange(hScene, boss.mid, boss.range.x1, boss.range.y1, boss.range.x2, boss.range.y2, 1, boss.liveTime)
					System.broadcastTipmsg(Lang.Activity.t00167, ttScreenCenter)
				end
				
				return
			end
		end
	end
	Actor.sendTipmsg(sysarg, Lang.Activity.t00156, ttFlyTip)
	return 
end




table.insert(MainFnTable, EndHorseRace_Main)